package com.ly.honor.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.honor.entity.AmbitusGoods;
import com.ly.honor.entity.dto.ClueQueryDTO;
import com.ly.honor.entity.dto.GoodsCreateDTO;
import com.ly.honor.entity.dto.GoodsEditDTO;
import com.ly.honor.entity.dto.GoodsQueryDTO;
import com.ly.honor.entity.vo.GoodsQueryVO;
import com.ly.honor.service.AmbitusGoodsService;
import com.ly.honor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * (AmbitusGoods)表控制层
 *
 * @author makejava
 * @since 2023-03-31 17:19:22
 */
@RestController
@RequestMapping("goods")
@Validated
@Api(tags = "周边商品管理")
public class AmbitusGoodsController {
    /**
     * 服务对象
     */
    @Resource
    private AmbitusGoodsService ambitusGoodsService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @PostMapping("getGoodsPage/{pageNum}/{pageSize}")
    @ApiOperation("分页查询")
    public Result getGoodsPage(@PathVariable("pageNum") long pageNum,
                               @PathVariable("pageSize") long pageSize,
                               @RequestBody(required = false) GoodsQueryDTO goodsQueryDTO) {
        Page<GoodsQueryVO> goodsQueryVOPage = ambitusGoodsService.getGoodsPage(pageNum,pageSize,goodsQueryDTO);
        return Result.ok(goodsQueryVOPage);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("getGoodsById/{id}")
    public Result getGoodsById(@PathVariable("id") Integer id) {
        GoodsQueryVO goodsQueryVO = ambitusGoodsService.getGoodsById(id);
        return Result.ok(goodsQueryVO);
    }

    /**
     * 新增数据
     *
     * @param goodsCreateDTO 实体
     * @return 新增结果
     */
    @PostMapping("addGoods")
    @ApiOperation("新增商品")
    public Result addGoods(@RequestBody @Valid GoodsCreateDTO goodsCreateDTO) {
        ambitusGoodsService.addGoods(goodsCreateDTO);
        return Result.ok();
    }

    /**
     * 编辑数据
     *
     * @param goodsEditDTO 实体
     * @return 编辑结果
     */
    @PostMapping("updateGoods")
    @ApiOperation("编辑数据")
    public Result updateGoods( @RequestBody @Valid GoodsEditDTO goodsEditDTO) {
        ambitusGoodsService.updateGoods(goodsEditDTO);
        return Result.ok();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("deleteGoodsById/{id}")
    @ApiOperation("通过id删除数据")
    public Result deleteGoodsById(Integer id) {
        ambitusGoodsService.removeById(id);
        return Result.ok();
    }

    @DeleteMapping("removeBatch")
    @ApiOperation("批量删除商品")
    public Result removeBatch(@RequestBody List<Long> idList) {
        ambitusGoodsService.removeByIds(idList);
        return Result.ok();
    }

    @PostMapping("upload")
    @ApiOperation("文件上传")
    public Result uploadFile(MultipartFile file){
        String url = ambitusGoodsService.upload(file);
        return Result.ok(url).message("上传文件成功");
    }

}

